package com.example.module_motorcircle.detail

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.Router
import com.example.lib_common.base.BaseToolBarActivity
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.state.UIState
import com.example.lib_common.utils.ToastUtils
import com.example.module_motorcircle.R
import com.example.module_motorcircle.databinding.ActivityPostDetailBinding
import com.example.module_motorcircle.detail.adapter.PostDetailAdapter
import com.example.module_motorcircle.detail.viewmodel.PostDetailViewModel
import com.example.module_motorcircle.dialog.CommentDialog
import dagger.hilt.android.AndroidEntryPoint

/**
 * 帖子详情：一个recyclerview多布局展示3个接口中数据
 */
@AndroidEntryPoint
@Route(path = Router.MOTOR_CIRCLE_POST_DETAIL_ACTIVITY)
class PostDetailActivity : BaseToolBarActivity<PostDetailViewModel,ActivityPostDetailBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_post_detail
    override val mViewModel: PostDetailViewModel by viewModels()

    @Autowired
    lateinit var id:String

    private var commentDialog:CommentDialog? = null

    private val mAdapter by lazy {
        PostDetailAdapter(
            onClickComments = {post,isFirst ->
                mViewModel.postCommentModel.parentId = post.postsCommentId
                mViewModel.postCommentModel.postsId = post.postsId
                mViewModel.postCommentModel.beReplyId = post.createBy
                if(isFirst) {//回复一级评论
                    mViewModel.postCommentModel.firstCommentId = post.postsCommentId
                }else{ //回复二级评论
                    mViewModel.postCommentModel.firstCommentId = post.firstCommentId
                }
                showCommentDialog(String.format("回复%s",post.requireCreateName())) //hint:评论用户名称
            }
        )
    }

    override fun initData() {
        mViewModel.loadPostDetailData(id)

    }
    override fun initView() {
        super.initView()
        initToolBar()
        if(id.isNullOrEmpty()){
            ToastUtils.showLong(this,"帖子id为空")
            finish()
        }
        initAdapter()
    }
    fun initAdapter() {
        mBinding.rv.layoutManager = LinearLayoutManager(this)
        mBinding.rv.adapter = mAdapter
    }
    fun initToolBar() {
       mBinding.appBar.toolbarTitle.text = "帖子详情"
    }

    override fun initListener() {
        mViewModel.initState_.observeWithLifecycle(this){
            when(it){
                is UIState.Loading -> {
                   setLoadingViewVisible(true)
                }
                is UIState.Success -> {
                    setLoadingViewVisible(false)
                    mAdapter.setPostInfo(mViewModel.currentPost!!)
                    mAdapter.setAfterSaleInfo(mViewModel.afterSaleInfo!!)
                    mAdapter.setCommentInfo(mViewModel.commentist!!,mViewModel.currentPost?.commentNumber!!)
                    mBinding.post = mViewModel.currentPost!! //展示下方UI
                    Log.d("PostDetailActivity", "currentPost: ${mViewModel.currentPost}")
                    Log.d("PostDetailActivity", "commentist: ${mViewModel.commentist}")
                    Log.d("PostDetailActivity", "afterSaleInfo: ${mViewModel.afterSaleInfo}")
                }
                is UIState.Error -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"失败 ")
                }
                else -> {}
            }
        }
        //订阅发布评论结果
        mViewModel.commentPostState_.observeWithLifecycle(this){
            when(it){
                is UIState.Loading -> {
                    setLoadingViewVisible(true)
                }
                is UIState.Success -> {
                    setLoadingViewVisible(false)
                    mViewModel.loadPostDetailData(mViewModel.currentPost?.postsId!!)
                    //优化

                }
                is UIState.Error -> {
                    setLoadingViewVisible(false)
                    ToastUtils.showLong(this,"发布失败")
                }
                else -> {}
            }
        }
        //点击弹出评论:1.校验  2.弹出评论框  3.输入内容留言帖子
        mBinding.lEdit.setOnClickListener {
            //1.校验
            mViewModel.commentValidate(mViewModel.currentPost?.postsId!!,mViewModel.currentPost?.createBy!!){
                //2.弹出评论框
                showCommentDialog()
            }
        }
    }

    private fun showCommentDialog(hint:String? = null){
        this.commentDialog = CommentDialog(
            hint = hint,
            onClickSend = {
                //3.发布帖子
                sendComment(it)
            }
        )
        this.commentDialog?.show(supportFragmentManager)
    }
    //发布帖子
    private fun sendComment(content:String){
        mViewModel.postCommentSave(content)
        this.commentDialog?.dismiss()
    }

}